var express = require('express');
var router = express.Router();

//引入数据库模块
const connection = require('./conn');
//连接数据库
connection.connect( () =>{
  console.log('数据库连接成功');
})

//接收登录请求
router.post('/checklogin',(req, res) => {
  //后端接收用户名与密码
  let {username, password} = req.body;
  //去数据库查询是否存在这个账号的用户
  //sql查询
  const sqlStr = `select * from users where username = '${username}' and password = '${password}'`;
  connection.query(sqlStr,(err,data) => {
    if(err){
      throw err;
    }else {
      res.send(data);
      //console.log(data)
    }
  })
})

//接收注册请求
router.post('/register',(req, res) => {
  //后端接收用户注册填写的信息
  //sql查询
  const sqlStr = `insert into users(username,password,sex,introduction) values('${req.body.username}','${req.body.password}','${req.body.sex}','${req.body.introduction}')`;
  connection.query(sqlStr,(err,data) => {
    if(err){
      throw err;
    }else {
      res.send(data);
      console.log(data)
    }
  })
})

//查询某种商品列表
router.post('/getNewsList',(req,res) =>{
  // let page = req.body.Page;
  // let pageSize = req.body.pageSize;
  const sqlStr = `select * from  relatedProducts where type = '${req.body.type}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err;
    }else {
      // let result = data.slice((page-1)*pageSize,page*pageSize)
      res.send(data);
      //console.log(data)//
    }
  })
})
//查询某种文章短评的列表
router.post('/getArticlesList',(req,res) =>{
  const sqlStr = `select * from  articles where type = '${req.body.type}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err;
    }else {
      res.send(data);
      //console.log(data)//
    }
  })
})
//某个用户发布商品
router.post('/addNews',(req,res) =>{
  const sqlStr = `insert into relatedProducts(type,title,describes,username) values('${req.body.type}','${req.body.name}','${req.body.content}','${req.body.username}')`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
        res.send(data)
        console.log(data + '11111')
      }
  })
})
//某个用户发布短评
router.post('/addArticles',(req,res) =>{
  const sqlStr = `insert into articles(type,title,content,author,liked) values('${req.body.type}','${req.body.name}','${req.body.content}','${req.body.username}',0)`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
        res.send(data)
        console.log(data + '11111')
      }
  })
})
//查询某个商品的详情
router.post('/getNewsDetail',(req,res) =>{
  const sqlStr = `select * from  relatedProducts where id = '${req.body.id}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err;
    }else {
      res.send(data);
      //console.log(data)
    }
  })
})
//查询商品的评论
router.post('/getNewsComments',(req,res) =>{
  const sqlStr = `select * from  comments where whichProducts = '${req.body.id}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err;
    }else {
      res.send(data);
      //console.log(data)
    }
  })
})
//增加评论
router.post('/addComments',(req,res) =>{
  const sqlStr = `insert into comments(content,whichProducts,author) values('${req.body.content}','${req.body.id}','${req.body.author}')`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
        throw err;
    }else{
        res.send(data)
        console.log(data + '11111')
      }
  })
})
//查询某商品被哪些用户收藏
router.post('/getCollectedUsers',(req,res) =>{
  //
  const sqlStr = `select users.* from users left join collected on users.id = collected.uid where collected.pid = '${req.body.id}';`
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
      res.send(data)
      //console.log(data)//一个包含所有收藏用户的对象数组
    }
  })
})
//某个用户收藏某商品
router.post('/addCollection',(req,res) =>{
  const sqlStr = `insert into collected values('${req.body.uid}','${req.body.pid}')`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      try{
        errno == 1062;
      }catch{
        data = '您已经收藏过该商品了！！'
        res.send(data)
        console.log(data)
      }
    }else{
        res.send(data)
        console.log(data + '11111')
      }
  })
})
//某个用户点赞某短评
router.post('/addLiked',(req,res) =>{
  const sqlStr = `update articles set liked = '${req.body.likedNum}' where id ='${req.body.id}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
        res.send(data)
        console.log(data)
      }
  })
})
//查询用户所收藏的商品
router.post('/getCollectedProductsData',(req,res) =>{
  //
  const sqlStr = `select relatedProducts.* from relatedProducts left join collected on relatedProducts.id = collected.pid where collected.uid = '${req.body.id}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
      res.send(data)
      //console.log(data)//一个包含所有收藏用户的对象数组
    }
  })
})
//查询用户所发布的商品
router.post('/getPublishedProductsData',(req,res) =>{
  //
  const sqlStr = `select relatedProducts.* from relatedProducts  where relatedProducts.username = '${req.body.username}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
      res.send(data)
      //console.log(data)//一个包含所有收藏用户的对象数组
    }
  })
})
//查询用户所发布的短评
router.post('/getPublishedArticlesData',(req,res) =>{
  const sqlStr = `select articles.* from articles  where articles.author = '${req.body.username}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
      res.send(data)
      //console.log(data)//一个包含所有收藏用户的对象数组
    }
  })
})
//编辑用户信息
router.post('/updateUserInfo',(req,res) =>{
  const sqlStr = `update users set username = '${req.body.username}' ,sex = '${req.body.sex}',introduction = '${req.body.introduction}'  where id ='${req.body.id}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
        res.send(data)
        console.log(data)
      }
  })
})
//修改用户密码
router.post('/changeUserPwd',(req,res) =>{
  const sqlStr = `update users set password = '${req.body.password}'  where id ='${req.body.id}'`;
  connection.query(sqlStr,(err,data) =>{
    if(err){
      throw err
    }else{
        res.send(data)
        console.log(data)
      }
  })
})

module.exports = router;